home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’93 / Happy Happy, Joy Joy / HappyHappy.c < prev    next >
Text File  |  1993-06-18  |  3KB  |  181 lines

  1. /*
  2.     HappyHappy.c
  3.     
  4.     HappyHappy extension. Demonstrates use of PatchWorks trap patching
  5.     system to patch DrawString. Uses new GenericPatch class.
  6.     
  7.     by Mouse Herrell & Patrick Beard.
  8.     
  9.     © 1991 Berkeley Systems Inc.
  10. */
  11.  
  12. #include <string.h>
  13. #include <Notification.h>
  14. #include <Traps.h>
  15. #include <stdarg.h>
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <stddef.h>
  19.  
  20. #include <Exceptions.h>
  21. #include <Extension.h>
  22. #include <GenericPatch.h>
  23.  
  24. static int dprintf(const char* format, ...);
  25.  
  26. //
  27. //    DrawStringPatch -- HappyHappy of a head-off patch.
  28. //
  29.  
  30. class DrawStringPatch : public GenericPatch {
  31. public:
  32.     DrawStringPatch();
  33.     virtual void Behavior(void);
  34.  
  35. private:
  36.     long itsCallCount;
  37. };
  38.  
  39. struct DrawStringParameters {
  40.     Str255 *theString;
  41. };
  42.  
  43. typedef struct DrawStringParameters DrawStringParameters;
  44.  
  45. typedef pascal long (*DrawStringProcPtr) (Point pt);
  46.  
  47. DrawStringPatch::DrawStringPatch()
  48. {
  49.     // initialize instance variables.
  50.     itsCallCount = 0;
  51.     
  52.     // install the appropriate patch.
  53.     GenericPatch::InitGenericPatch(_DrawString, sizeof(DrawStringParameters));
  54.     Install();
  55. }
  56.  
  57. void DrawStringPatch::Behavior()
  58. {
  59.     KeyMap Keys;
  60.     int        theLen, index;
  61.     char     *buffer;
  62.     int        count, Happy;
  63.     
  64.     // announce our presence if control key held down.
  65.     //GetKeys(Keys);
  66.     //if (Keys[1] & 0x8) {
  67.     if (1) {   // normally we would check for a message from the Happy Button
  68.         DrawStringParameters* params = (DrawStringParameters*)itsFrame->parameters;
  69.                 
  70.         // head off the patch and return.
  71.         //AbortTrap();
  72.         
  73.         theLen = *(params->theString)[0];
  74.         buffer = (char *)(params->theString);
  75.         
  76.         //dprintf("DrawString:  params = (%ld), theLen = %x, buffer = %ld",params,theLen,buffer);
  77.         count = 0;
  78.         Happy = 0;
  79.         
  80.         if (theLen > 3)
  81.         
  82.         for (index=1; index <= theLen; index++)
  83.             {
  84.             if (isalpha(buffer[index]))
  85.                 {
  86.                     if (count == 0)
  87.                         if (Happy)
  88.                             buffer[index] = 'J';
  89.                         else 
  90.                             buffer[index] = 'H';
  91.                     else if (count == 1)
  92.                         if (Happy)
  93.                             buffer[index] = 'o';
  94.                         else
  95.                             buffer[index] = 'a';
  96.                     else if (count == 2)
  97.                         if (Happy)
  98.                             buffer[index] = 'o';
  99.                         else
  100.                             buffer[index] = 'p';
  101.                     else
  102.                         if (Happy)
  103.                             buffer[index] = 'o';
  104.                         else
  105.                             buffer[index] = 'p';
  106.                     count++;
  107.                                         
  108.                     if ((ispunct(buffer[index+1])) || (isspace(buffer[index+1])))
  109.                         buffer[index] = 'y';
  110.                 
  111.                 
  112.                 }
  113.             else if (ispunct(buffer[index]))
  114.                 {
  115.                 count = 0;
  116.                 if (Happy)
  117.                     Happy = 0;
  118.                 else
  119.                     Happy = 1;
  120.                 }
  121.             else if (isspace(buffer[index]))
  122.                 {
  123.                 count = 0;
  124.                 if (Happy)
  125.                     Happy = 0;
  126.                 else
  127.                     Happy = 1;
  128.                 }
  129.             }
  130.         buffer[theLen] = 'y';
  131.         
  132.     }
  133. }
  134.  
  135. //
  136. //    Install routine. This is where you allocate your patch objects.
  137. //    Throw an exception if something fails.
  138. //
  139.  
  140. void Install()
  141. {
  142.     try {
  143.         // if extension succeeds, show happy icon.
  144.         new DrawStringPatch;
  145.         ShowIconFamily(128);
  146.     } catch {
  147.         // extension failed, show sad icon.
  148.         ShowIconFamily(129);
  149.         throw(theException);
  150.     }
  151. }
  152.  
  153. //
  154. //    Remove routine. This is called when system is shutdown.
  155. //    Perhaps this should be removed. Rob thinks it shouldn't even be
  156. //    part of the design.
  157. //
  158.  
  159. void Remove()
  160. {
  161.     Patch::RemoveAll();
  162. }
  163.  
  164. //
  165. // debugging printf.
  166. //
  167.  
  168. static int dprintf(const char* format, ...)
  169. {
  170.     va_list args;
  171.     static char str[256];
  172.     int count;
  173.     
  174.     va_start(args, format);
  175.     count = vsprintf(str, format, args);
  176.     va_end(args);
  177.     DebugStr(c2pstr(str));
  178.         
  179.     return count;
  180. }
  181.